﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Reflection;
using LinqToBing.Bing.Wrapper.Interfaces;
using LinqToBing.Bing.Wrapper.Enums;
using System.Xml.Schema;

namespace LinqToBing.Bing.Wrapper.Search
{
    public abstract class SearchResultBase : ISearchResult, ISearchXDocumentResult
    {
        #region ISearchResult implementation
        /// <summary>
        /// The search string.
        /// </summary>
        public string Query { get; set; }
        
        /// <summary>
        /// The sources on which execute the search (Web, Images, etc.).
        /// </summary>
        /// <remarks>
        /// The search can be executed on a combination of multiple values.
        /// </remarks>
        public List<Enums.SearchSource> Sources { get; set; }

        private int count = 0;
        /// <summary>
        /// Number of value required for the search.
        /// </summary>
        /// <remarks>
        /// This is an optional value.
        /// </remarks>
        public int Count 
        { 
            get
            {
                return count;
            }
            set
            {
                count = (value >= 0) ? value : 0;
            }
        }

        private int offset = 0;
        /// <summary>
        /// Number of result items to skip for the search.
        /// </summary>
        /// <remarks>
        /// Used for pagination.
        /// </remarks>
        public int Offset
        { 
            get
            {
                return offset;
            }
            set
            {
                offset = (value >= 0) ? value : 0;
            }
        }
        #endregion

        /// <summary>
        /// A title to assign for the search.
        /// </summary>
        /// <remarks>
        /// It is an optional value, and is not present in the Bing search engine
        /// </remarks>
        public string Title { get; set; }

        /// <summary>
        /// The url that is composed to execute the search.
        /// </summary>
        public Uri Url
        {
            get;
            set;
        }
        public string InvokeUrl
        {
            get 
            {
                string Result = string.Format("{0}?{1}", "", "");

                return Result;
            }
        }

        #region ISearchXDocumentResult implementation
        private XDocument xDocument = null;
        public XDocument XDocument
        {
            get
            {
                return xDocument;
            }

            set
            {
                if (xDocument != value)
                {
                    xDocument = value;
                    //XmlSchemaSet schema = new XmlSchemaSet();
                    //schema.Add(Common.XMLNS.GetValidatingXsd(), );
                    //            xDocument.Validate()
                    //            XmlValidatingReader v = new XmlValidatingReader();
                }
            }
        }
        #endregion

        public abstract void ParseDocument(XDocument xdoc);

        /// <summary>
        /// Guid for uniquely identify the search.
        /// </summary>
        private Lazy<Guid> guid = new Lazy<Guid>();
        public Guid Guid
        {
            get { return guid.Value; }
        }


        public SearchResultHeader Header
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public SearchResultBody Body
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }




        public int Total
        {
            get { throw new NotImplementedException(); }
            set {throw new NotImplementedException();}
        }

        public string AppId
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }


        public SearchProtocol SearchProtocol
        {
            get;
            set;
        }
    }
}
